var host = "http://www.bitcufund.com/fund/";

$(document).ready(function() {
	alert(2222);
	Fund_init();
	New_init();
	buyModal();
	Fund_init1();
	Fund_init2();
	toFundDetail();
})

function Fund_init() {
	$("#fund_list").empty();
	$.ajax({
		"url": host + "/foundInfo/findslist",
		"type": "get",
		"async": false,
		"dataType": "json",
		"success": function(result) {
			$.each(result, function(index, element) {
				tableHtml_fund(element);
			});
		},
		"error": function() {
			//alert("出现错误！");
		}
	});
}

function tableHtml_fund(element) {
	var itemHTML = "<tr>" +
		"<td align='left' class='fund_name'>" +
		"<div class='fund_bg'>" +
		"<input type='hidden' value="+element.id+">"+
		"<a target='_blank' class='fundName'>" + element.fund_name + "</a>" +
		"</div>" +
		"</td>" +
		"<td>" + element.code + "</td>" +
		"<td>" + element.net_time + "</td>" +
		"<td class='net_fund'>" + element.net_fund + "</td>" +
		"<td>" + element.seven_day_annualized + "</td>" +
		"<td><span class='green'>" + element.last_half_year + "</span></td>" +
		"<td><span class='green'>" + element.last_year + "</span></td>" +
		"<td><span class='red'>" + element.since_this_year + "</span></td>" +
		"<td><span class='green'>" + element.last_half_year + "</span></td>" +
		"<td><span class='red'>" + element.since_founding + "</span></td>" +
		"<td><button class='layui-btn layui-btn-warm buy_fund'>申购</button></td>" +
		"</tr>";
	$("#fund_list").append(itemHTML);
}

function prorityInit() {
	$.ajax({
		"url": host + "/foundInfo/???",
		"type": "get",
		"async": false,
		"dataType": "json",
		"success": function(result) {
			$.each(result, function(index, element) {
				tableHtml_fund(element);
			});
		},
		"error": function() {
			//alert("出现错误！");
		}
	});
}

function New_init() {
	$(".index_news_box ul").empty();
	var box1 = $(".index_news_box").eq(0);
	var c1 = 1;
	var box2 = $(".index_news_box:nth-of-type(2) ul");
	var c2 = 1;
	var box3 = $(".index_news_box:nth-of-type(3) ul");
	var c3 = 1;
	var box4 = $(".index_news_box:nth-of-type(4) ul");
	var c4 = 1;
	$.ajax({
		"url": host + "/indystryInfo/industrylist",
		"type": "get",
		"async": false,
		"dataType": "json",
		"success": function(result) {
			$.each(result, function(index, element) {
				var unixTimestamp = new Date(element.create_date);
				commonTime = unixTimestamp.toLocaleString();
				var itemHTML = "<li><span class='right'>" + commonTime + "</span><input type='hidden' value=" + element.id + "/><a href='newDetail.html'>" + element.title + "</a>&nbsp;</li>";
				switch(element.status) {
					case 1:
						if(c1 == 10) {
							break;
						} else {
							c1++;
							box1.append(itemHTML);
							break;
						}
					case 2:
						if(c2 == 10) {
							break;
						} else {
							c2++;
							box2.append(itemHTML);
							break;
						}
					case 3:
						if(c3 == 10) {
							break;
						} else {
							c3++;
							box3.append(itemHTML);
							break;
						}
					case 4:
						if(c4 == 10) {
							break;
						} else {
							c4++;
							box4.append(itemHTML);
							break;
						}
					default:
						break;
				}
			});
		},
		"error": function() {
			//alert("出现错误！");
		}
	});

}

function Fund_init1() {
	$.ajax({
		"url": host + "/foundInfo/findslist",
		"type": "get",
		"async": false,
		"dataType": "json",
		"success": function(result) {
			$.each(result, function(index, element) {
				$("#p1_name").text(element.fund_name);
				
				$("#p1_daily_increase").text(element.daily_increase);
				$("#p1_ten_thousand_income").text(element.ten_thousand_income);
				return;
			})
		},
		"error": function() {
			//alert("出现错误！");
		}
	});
}

function Fund_init2() {
	$.ajax({
		"url": host + "/foundInfo/findslist",
		"type": "get",
		"async": false,
		"dataType": "json",
		"success": function(result) {
			$.each(result, function(index, element) {
				
				switch(index) {
					case 0:
						var itemHtml = element.fund_name + "<span class='f13'>(" + element.code + ")</span>";
						$("#p2_1_name").html(itemHtml);
						break;
					case 1:
						var itemHtml = element.fund_name + "<span class='f13'>(" + element.code + ")</span>";
						$("#p2_2_name").html(itemHtml);
						$("#p2_2_seven_day_annualized").text(element.seven_day_annualized);
						$("#p2_2_ten_thousand_income").text(element.ten_thousand_income);
						break;
					case 2:
						var itemHtml = element.fund_name + "<span class='f13'>(" + element.code + ")</span>";
						$("#p2_3_name").html(itemHtml);
						$("#p2_3_last_year").text(element.last_year);
						$("#p2_3_daily_increase").text(element.daily_increase);
						break;
					case 3:
						var itemHtml = element.fund_name + "<span class='f13'>(" + element.code + ")</span>";
						$("#p2_4_name").html(itemHtml);
						$("#p2_4_last_half_year").text(element.last_half_year);
						$("#p2_4_since_this_year").text(element.since_this_year);
						break;
					default:
						break;
				}
			});
		},
		"error": function() {
			//alert("出现错误！");
		}
	});
	$.ajax({
		"url": host + "/foundInfo/findslist",
		"type": "get",
		"async": false,
		"dataType": "json",
		"success": function(result) {
			$.each(result, function(index, element) {
				var itemHtml = element.fund_name + "<span class='f13'>(" + element.code + ")</span>";
				$("#p2_1_name").html(itemHtml);
			});
		},
		"error": function() {
			//alert("出现错误！");
		}
	});

}

function buyModal() {
	$('.buy_fund').click(function() {
		layer.open({
			title: '申购',
			type: 1,
			skin: 'layui-layer-demo', //样式类名
			closeBtn: 0, //不显示关闭按钮
			anim: 2, //弹出层样式
			shadeClose: true, //开启遮罩关闭
			area: ['40%', '500px'],
			content: "<div class='' id='a_bodyDiv'>" +
				"<div class='tit'><div><span>基金名称：</span></div><span id='fund_name'>链想基金一号</span></div>" +
				"<div class='tit'><div><span>单位净值：</span></div><span id='fund_asset'>21111</span></div>" +
				"<div class='tit'><div><span>申购份额：</span></div><input type='text' id='fund_share' /></div>" +
				"<div class='tit'><div><span></span></div><span id='fund_total'>总额:<span class='btc_large' id='fund_total_usdt'>2000.12</span>&nbsp;USDT&nbsp;&nbsp;(手续费:<span class='btc_large' id='fund_charge'>200</span>USDT)</span></div>" +
				"<div class='tit'><div><span>支付方式:</span></div><select><option value='1'>BTC</option><option value='2'>ETC</option><option value='3'>LTC</option><select></div>" +
				"<div class='tit'><div><span></span></div><span id='name'>所需<span>BTC</span>数量:&nbsp;&nbsp;<span id='end_total'>1.25555</span></span></div>" +
				"<div class='tit'>申购后请联系在线客服进行最后确认以及支付!</div>" +
				"<div class='tit'><input type='button' value='确认申购' id='buyFund'/></div>" +
				"</div>"
		});
		 var tr=$(this).parent().parent();
		 var fund_name=tr.find(".fundName").text();
		 var net_fund=tr.find(".net_fund").text();
		 $("#fund_name").text(fund_name);
		 $("#fund_asset").text(fund_name);
	});
}

function toFundDetail(){
	$(".fundName").on("click",function(){
		var id=$(this).prev().val();
		location.href="fundProduct.html?id="+id;
	})
}
